
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Split {
    
    public static void main(String[] args) {
        Split split = new Split();
        split.split();
    }
    
    public void split() {
        int linesPerFile = 500000; //100,000
        int filenum = 1;
        int linenum = 0;
        
        File infile = new File("F:/splitTrain/TrainingTweets.csv");
        
        String directory = "F:/splitTrain/splittedTrainingSet/";
        String filename = "splittedTrainingTweets";
        String extension = ".csv";
        File outfile = new File(directory + filename + filenum + extension);
                
        try {
            BufferedReader reader = new BufferedReader(new FileReader(infile));
            BufferedWriter writer = new BufferedWriter(new FileWriter(outfile));
            
            String line = reader.readLine();
            while (line != null) {
                if (linenum == linesPerFile) {
                    System.out.println("files: " + filenum);
                    writer.close();
                    filenum++;
                    outfile = new File(directory + filename + filenum + extension);
                    writer = new BufferedWriter(new FileWriter(outfile));
                    linenum = 0;
                }
                writer.write(line + "\n");
                linenum++;
                line = reader.readLine();
            }
            
            System.out.println("files: " + filenum);
            reader.close();
            writer.close();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}
